

using System;
using System.Data;
using System.Web;
using System.Web.UI;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using PS.WebParts.Rollup.Core;

namespace PS.WebParts.Rollup
{
    internal class AggregateDataView : ATabularDataSourceView
    {
        string dataSourcesString;
        string aggregate;
        AggregateDataSource owner;
        DataSourceCollection sourceCollection;
        string languageVersion;
  
        public AggregateDataView(HttpContext context, AggregateDataSource owner)
            : base(context, owner, string.Empty)
        {
            this.owner = owner;
        }
        
        public AggregateDataView(AggregateDataSource source) : base (HttpContext.Current, source, string.Empty)
        {
            owner = source;
        }

        public string LanguageVersion
        {
            get { return languageVersion; }
            set { languageVersion = value; }
        }

        public string DataSourcesString
        {
            get {

                dataSourcesString = SerializeDatasources(sourceCollection);
                return dataSourcesString;
            }
            set { dataSourcesString = value; }
        }

        private static string SerializeDatasources(DataSourceCollection sourceCollection)
        {
        
            DataFormWebPart dfwp = new DataFormWebPart();
            foreach (IDataSource source in sourceCollection)
            {
                dfwp.DataSources.Add(source);
            }
            return dfwp.DataSourcesString;
        }

        public string Aggregate
        {
            get { return aggregate; }
            set { aggregate = value; }
        }

        public DataSourceCollection GetDataSources()
        {
            if (sourceCollection == null) sourceCollection = new DataSourceCollection();
            
            if (sourceCollection.Count == 0 && !string.IsNullOrEmpty(dataSourcesString))
            {
                Page page = owner.Page;
                if (page.AppRelativeVirtualPath == null)
                {
                    page.AppRelativeVirtualPath = "~/current.aspx";
                }

                Control control = page.ParseControl(dataSourcesString.Trim());
                foreach (Control ctl in control.Controls)
                {
                    if (ctl is ATabularDataSource)
                    {
                        sourceCollection.Add(ctl as ATabularDataSource);
                    }
                }
            }

            return sourceCollection;
        }

        public override DataTable GetData()
        {
            try
            {
                DataTable results = Process();

                results.TableName = owner.RowsName;

                return results;
            }
            catch (Exception e)
            {
                LogError("An error has occured while aggregating datasources", e);
                return null; //e.ToString();
            }
        }

        private DataTable Process()
        {
            SetOperation union;
            if (!string.IsNullOrEmpty(this.aggregate))
                union = AggregateParser.Parse(this.aggregate);
            else
            {
                union = new Union();
                foreach (Control source in this.GetDataSources())
                {
                    union.Children.Add(new DataTableRef(source.ID));
                }
            }
            DataSourceCollection sources = this.GetDataSources();
            base.SetCachedDataSources(sources);
            AggregateInterpreter interpreter = new AggregateInterpreter(union, sources);

            return interpreter.GetResults();
        }

        public override DataTable GetSchema()
        {
            throw new NotImplementedException();
        }
    }
}
